//
//  CaseDetailsViewController.m
//  ForestPack
//
//  Created by jimmy on 2018/7/10.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "CaseDetailsViewController.h"
#import "FPPublicConmmandVC.h"
#import "FPCommodityDetailCell.h"
#import "FPCommodityHeaderView.h"
#import "HMsegmentedControl.h"
#import "Masonry.h"
#import "UIView+Utils.h"
#import "FPBottomButtonView.h"
#import "FPCustomButton.h"
#import "CaseDetailModel.h"

@interface CaseDetailsViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView    * tableView;
///案例详情
@property (nonatomic,strong)  CaseDetailModel  * caseDetailModel;
@property (nonatomic,strong)  NSMutableArray   * caseDetailArray;

@property (nonatomic,strong)  NSString                 * webContent;
@property (nonatomic,assign)  CGFloat                    webHeight;

@end

@implementation CaseDetailsViewController

- (NSMutableArray *)caseDetailArray
{
    if (_caseDetailArray == nil) {
        _caseDetailArray = [[NSMutableArray alloc] init];
    }
    return _caseDetailArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"案例详情"];
    [self setRightButton:[UIImage imageNamed:@"更多。"] title:nil target:self action:@selector(moreClick)];
    [self setTableViewUI];
    [self setBottomViewUI];
    
    [self requestCaseDetailData];
}

#pragma mark - 请求案例详情接口数据
-(void)requestCaseDetailData
{
    NSString * url = [NSString stringWithFormat:@"%@app/case_detail",REQUESTHEADER];
    NSDictionary * parameter = @{@"id":@(self.design_id)};
    [HttpManager showWait];
    WeakObj(self);
    [HttpManager postHttpRequestByPost:url andParameter:parameter success:^(id successResponse) {
        [HttpManager hideWait];
        [selfWeak.tableView.mj_header endRefreshing];
        NSDictionary *dict = successResponse;
        NSString *status = [NSString stringWithFormat:@"%@",dict[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSError *error;
            // 成功
            selfWeak.caseDetailModel = [[CaseDetailModel alloc] initWithDictionary:dict error:&error];
            
            if (selfWeak.caseDetailModel != nil) {
                [selfWeak.caseDetailArray addObject:self.caseDetailModel];
                [selfWeak.tableView reloadData];
            }
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [selfWeak.tableView.mj_header endRefreshing];
        [HttpManager showFail];
    }];
}

-(void)moreClick
{
    
}

-(void)setTableViewUI
{
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - 49) style:UITableViewStyleGrouped];
    //    UITableViewStylePlain, cell不会跟着区头滑动
    //    UITableViewStyleGrouped  cell会跟着区头一起滑动
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

-(void)setBottomViewUI
{
    FPBottomButtonView *buttonView = [FPBottomButtonView initBottomButtonViewWithTitle:@"让他为我服务"];
    buttonView.frame = CGRectMake(0, self.view.frame.size.height - 49, SCREEN_WIDTH, 49);
    buttonView.threeTextButton.hidden = YES;
    buttonView.fiveButton.hidden = YES;
    buttonView.sixButton.hidden = YES;
    buttonView.payAttentionClick = ^{
        [HttpManager showWait];
        [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/collect_shop",REQUESTHEADER] andParameter:@{@"token":[FPLoginAndRegisterTools getToken],@"shop_id":self.caseDetailModel.shop.Id} success:^(id successResponse) {
            NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
            [HttpManager hideWait];
            if ([status isEqualToString:@"1"]) {
                [HttpManager showNoteMsg:@"操作成功"];
            }else{
                [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
            }
        } andFailure:^(id failureResponse) {
            [HttpManager showFail];
        }];
    };
    [self.view addSubview:buttonView];
    if (iPhoneX) {
        [buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.bottom.equalTo(self.view.mas_bottom).offset(TAB_BAR_HEIGHT - 15);
            make.height.equalTo(@49);
        }];
    }else{
        [buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.bottom.equalTo(self.view.mas_bottom);
            make.height.equalTo(@49);
        }];
    }
    
    buttonView.employClick = ^{
        //跳转到发布需求界面
        FPPublicConmmandVC *publicVC = [[FPPublicConmmandVC alloc] init];
        publicVC.design_Id = [self.caseDetailModel.data.Id integerValue];
        [self.navigationController pushViewController:publicVC animated:true];
    };
}

#pragma mark - UITableViewDelegate,DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return 1;
    }else{
        return 1;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 0;
    }else if (indexPath.section == 1){
        return 0;
    }else{
        
        return _webHeight;
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 280;
    }else if(section ==1){
        return 90;
    }else{
        return 46;
    }
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        FPCommodityHeaderView * sectionView = [FPCommodityHeaderView sectionViewWithHeight:280];
        sectionView.backgroundColor = [UIColor whiteColor];
        sectionView.userInteractionEnabled = YES;
        [sectionView initCaseDetailDataModel:self.caseDetailModel.data];
        return sectionView;
    }else if (section ==1){
        DesignSecondSectionHeaderView * sectionView = [DesignSecondSectionHeaderView sectionHeaderViewWithHeight:90];
        sectionView.backgroundColor = [UIColor whiteColor];
        sectionView.userInteractionEnabled = YES;
        [sectionView initCaseDetailShopModel:self.caseDetailModel.shop];
        return sectionView;
    }
    else{
        DesignFourSectionHeaderView * sectionView = [DesignFourSectionHeaderView sectionHeaderViewWithHeight:46];
        sectionView.backgroundColor = [UIColor whiteColor];
        sectionView.userInteractionEnabled = YES;
        return sectionView;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        return 49;
    }else{
        return 10;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    if (section == 1) {
        UIView *view = [[UIView alloc] init];
        UIView *bjView = [[UIView alloc] init];
        bjView.backgroundColor = [UIColor whiteColor];
        [view addSubview:bjView];
        [bjView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_top).offset(1);
            make.left.equalTo(view.mas_left);
            make.width.equalTo(view.mas_width);
            make.height.equalTo(@39);
        }];
        
        FPRightLeftButton *button = [FPRightLeftButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"让他为我服务" forState:UIControlStateNormal];
        [button setTitleColor:RGB_COLOR(237,34,59) forState:UIControlStateNormal];
        button.titleLabel.font = Kfont(14);
        [button setImage:[UIImage imageNamed:@"客服"] forState:UIControlStateNormal];
        [bjView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bjView.mas_top);
            make.left.equalTo(bjView.mas_left).offset(30);
            make.right.equalTo(bjView.mas_right).offset(-30);
            make.height.equalTo(bjView.mas_height);
        }];
        return view;
    }else{
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
        return view;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        FPCommodityFourCell *commodityFourCell = [tableView dequeueReusableCellWithIdentifier:@"FPCommodityFourCell"];
        if (commodityFourCell == nil) {
            commodityFourCell = [FPCommodityFourCell cellWithTableView:tableView andIndexPath:indexPath];
            
            __weak typeof(self) weakSelf = self;
            commodityFourCell.returnHeight = ^(CGFloat height) {
                weakSelf.webHeight = height;
                [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            };
        }
        
        if (self.caseDetailArray.count > 0) {
            commodityFourCell.caseDetailModel = self.caseDetailArray[0];
        }
        WeakObj(self);
        commodityFourCell.returnHeight = ^(CGFloat height){
            selfWeak.webHeight = height;
            [selfWeak.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            NSLog(@"%f",height);
        };
    
        
        return commodityFourCell;
        
    }else{
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        return cell;
    }
    
}

@end
